//
//  SEFollowedViewModel.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/25.
//

import UIKit

class SEFollowedViewModel: SEBaseViewModel {
    var followedList: [SEFollowedList] = []
    
    private var role: Int = 1
    
    /// 关注的分类 1 - 医院 2 - 医生
    convenience init(role: Int) {
        self.init()
        self.role = role
    }
    
    var numberOfSections: Int { followedList.count }
    
    func viewModel(at indexPath: IndexPath) -> SEFollowedCellViewModel {
        return SEFollowedCellViewModel(model: followedList[indexPath.section])
    }

    var selectedModel = SEFollowedList()

    var followedHandler: ((SEHttpStatus) -> Void)?
    
    /// 获取关注的列表
    func getListOfFollowed(loadMore: Bool) {
        SEHttpManager.shared.request(SEProfileAPI.listOfFollowedDoctors(role: role, pageNum: page, pageSize: pageSize), model: SEFollowedModel.self) { (model, httpStatus) in
            switch httpStatus {
            case .success:
                if !loadMore {
                    self.followedList = model?.list ?? []
                } else {
                    self.followedList.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = model?.totalPage ?? 0 > self.page
            default: break
            }
            self.updateData?(httpStatus)
        }
    }

    /// 关注/取消关注医生/医院
    func requestFollowed() {
        SEHttpManager.shared.requestForAny(SEProfileAPI.followedDoctorOrHospital(userId: UserDefaults.userId, targetId: selectedModel.id ?? "", role: "\(role)"), withLoading: true) { result, status in
            self.followedHandler?(status)
        }
    }
}

class SEFollowedCellViewModel: NSObject {
    var model: SEFollowedList

    var goodAt: String? {
        return (model.description ?? "").isEmpty ? model.description : "擅长: \(model.description ?? "")"
    }
    
    var avatarPlaceholder: UIImage {
        model.sex == 1 ? Asset.iconDoctorAvatarMale.image : Asset.iconDoctorAvatarFemale.image
    }
    
    init(model: SEFollowedList) {
        self.model = model
    }
}
